package com.flightcloud.ssm;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import com.flightcloud.ssm.common.handler.SchedulerErroHandler;
import com.flightcloud.ssm.configure.SimpleAsyncConfigure;
import com.flightcloud.ssm.configure.SimpleSchedulingConfigurer;

@EnableAsync
@EnableScheduling
public class AsyncConfig {

	/**
	 * 
	 * 描述 任务执行器
	 * 
	 * @return
	 *
	 */
	@Bean /* (name = "taskExecutor") */
	public TaskExecutor taskExecutor() {
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		taskExecutor.setCorePoolSize(5);
		taskExecutor.setKeepAliveSeconds(300);
		taskExecutor.setMaxPoolSize(10);
		taskExecutor.setQueueCapacity(30);
		return taskExecutor;
	}

	@Bean
	public AsyncConfigurer asyncConfigurer(@Qualifier("taskExecutor") TaskExecutor taskExecutor) {
		return new SimpleAsyncConfigure(taskExecutor);
	}

	@Bean /* (name = "taskScheduler") */
	public ThreadPoolTaskScheduler taskScheduler() {
		ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
		// 设置任务调度器线程池
		scheduler.setPoolSize(10);
		scheduler.setErrorHandler(new SchedulerErroHandler());
		return scheduler;
	}

	@Bean
	public SchedulingConfigurer scheduler(@Qualifier("taskScheduler") TaskScheduler taskScheduler) {
		return new SimpleSchedulingConfigurer(taskScheduler);
	}

}
